﻿using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace ORS.OrderRobot.WebApp.Filter
{
    /// <summary>
    /// 异常统一处理类
    /// </summary>
	public class ExceptionFilter : IExceptionFilter
    {

        public void OnException(ExceptionContext context)
        {
            var exception = context.Exception;

            // 创建统一的错误响应
            var errorResponse = new ErrorRes
            {
                Message = exception.Message,
                //Details = exception.StackTrace,
            };

            // 设置返回的结果
            context.Result = new ObjectResult(errorResponse)
            {
                StatusCode = 500
            };
            // 标记异常已处理，防止它传播到全局处理
            context.ExceptionHandled = true;
        }
    }
}

